package cn.hxzy.icon.icon.controller;

import cn.hxzy.icon.icon.entity.Resource;
import cn.hxzy.icon.icon.entity.Role;
import cn.hxzy.icon.icon.entity.po.ResourcePo;
import cn.hxzy.icon.icon.entity.vo.ReturnBean;
import cn.hxzy.icon.icon.service.ResourceService;
import cn.hxzy.icon.icon.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author little black
 */
@RestController
@RequestMapping("resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 查询全部
     *
     * @return
     */
    @PostMapping("resourceInfo")
    public ReturnBean resourceInfo() {
        List<Resource> all = resourceService.findAll();
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("deleteId")
    public ReturnBean deleteId(Integer id) {
        resourceService.delete(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 修改
     *
     * @param resourcePo
     * @return
     */
    @PostMapping("update")
    public ReturnBean update(ResourcePo resourcePo) {
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourcePo, resource);
        resourceService.updeat(resource);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 根据id查询全部
     * @param id
     * @return
     */
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Resource resource = resourceService.findById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, resource);
    }

    /**
     * 添加
     *
     * @param resource
     * @return
     */
    @PostMapping("add")
    public ReturnBean add(Resource resource) {
        resourceService.add(resource);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 未授权的
     * @param id
     * @return
     */
    @PostMapping("findByRoleIdNotHave")
    public ReturnBean findByRoleIdNotHave(Integer id) {
        List<Resource> resources= resourceService.findByRoleIdNotHave(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,resources);
    }


}
